import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>这是home组件 - {this.props.match?.params?.id}</div>;
  }
}

class About extends Component {
  render() {
    // console.log(this.props);
    const { search } = this.props.location; // "?a=3&b=4"
    const obj = new URLSearchParams(search);
    console.log(obj.get("a"));
    console.log(obj.get("b"));
    return <div>这是About组件</div>;
  }
}

const Mine = (props) => {
  console.log(props);
  return <div>这是我的组件</div>;
};

class App extends Component {
  state = {};
  render() {
    return (
      <>
        <h2>react-router-query</h2>
        <ul>
          <li>
            <Link to="/home/234">首页</Link>
          </li>
          <li>
            <Link to="/about?a=3&b=4">关于</Link>
          </li>
          <li>
            <Link
              to={{
                pathname: "/mine",
                search: "?x=10&y=20",
                hash: "#abc",
                // state一种在url上面不可见的传参
                state: {
                  name: "zhangsan",
                  age: 20,
                },
              }}
            >
              我的
            </Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path="/home/:id" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
